# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(PocoTCPServer)


# 输出路径变量
message(STATUS "Current source directory: ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "Project source directory: ${PROJECT_SOURCE_DIR}")
message(STATUS "Build directory: ${CMAKE_BINARY_DIR}")


# 定义构建根目录
set(BUILD_ROOT_SRC  /home/tronlong/A40i/LinuxSDK/out/a40i_h/tla40i-evm/buildroot/buildroot/build)


if( CROSS_COMPILE)#==============arm=================
    # 设置Poco库路径（从 buildroot target 获取）
    set(LIBRARY_DIR  /home/tronlong/A40i/LinuxSDK/out/a40i_h/tla40i-evm/buildroot/buildroot/staging/usr/lib)
    message(STATUS "==============arm=================")
    # 添加头文件搜索路径
include_directories(
    ${BUILD_ROOT_SRC}/poco-1.11.1/Foundation/include
    ${BUILD_ROOT_SRC}/poco-1.11.1/Net/include
    ${BUILD_ROOT_SRC}/mosquitto-2.0.14/include
    ${BUILD_ROOT_SRC}/zlog-1.2.15/src
    ${BUILD_ROOT_SRC}/jsoncpp-1.9.5/include/json
    ${BUILD_ROOT_SRC}/linenoise-1.0/
    src/

)
else()#==============ubuntu x86=================
message(STATUS "==============ubuntu x86=================")
    set(LIBRARY_DIR  
        /usr/lib/
        /usr/local/lib/
        /usr/lib/x86_64-linux-gnu/
        )
    # 添加头文件搜索路径
include_directories(
    /usr/include/
    src/
    /usr/include/jsoncpp/
)
endif()


# 查找库文件（手动指定，避免系统干扰）
find_library(POCO_NET_LIBRARY
    NAMES PocoNet
    PATHS ${LIBRARY_DIR}
    NO_DEFAULT_PATH
)

find_library(POCO_FOUNDATION_LIBRARY
    NAMES PocoFoundation
    PATHS ${LIBRARY_DIR}
    NO_DEFAULT_PATH
)



if(NOT POCO_NET_LIBRARY OR NOT POCO_FOUNDATION_LIBRARY)
    message(FATAL_ERROR "未找到 PocoNet 或 PocoFoundation 库！")
endif()

# 查找 mosquitto 库
find_library(MOSQUITTO_LIBRARY
    NAMES mosquitto
    PATHS ${LIBRARY_DIR}
    NO_DEFAULT_PATH
)

if(NOT MOSQUITTO_LIBRARY)
    message(FATAL_ERROR "未找到 mosquitto 库！")
endif()


# 查找 zlog 库
find_library(ZLOG_LIBRARY
    NAMES zlog
    PATHS ${LIBRARY_DIR}
    NO_DEFAULT_PATH
)

if(NOT ZLOG_LIBRARY)
    message(FATAL_ERROR "未找到 zlog 库！")
endif()



# 查找 JSONCPP  库
find_library(JSONCPP_LIBRARY
    NAMES jsoncpp
    PATHS ${LIBRARY_DIR}
    NO_DEFAULT_PATH
)

if(NOT JSONCPP_LIBRARY)
    message(FATAL_ERROR "未找到 jsoncpp 库！")
endif()

# 查找 linenoise 库
find_library(LINENOISE_LIBRARY
    NAMES linenoise
    PATHS ${LIBRARY_DIR}
    NO_DEFAULT_PATH
)

if(NOT LINENOISE_LIBRARY)
    message(FATAL_ERROR "未找到 linenoise 库！")
endif()



# 设置输出目录（可选）
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)



# 创建可执行文件
# add_executable(poco_tcp_server  demo/tcp_server.cpp)
# add_executable(mosquitto_client  mqtt_demo.cpp)
# add_executable(zlog_demo zlog_demo.cpp)
# add_executable(linenoise_demo linenoise_demo.cpp)

# 链接库
# target_link_libraries(poco_tcp_server
#     ${POCO_NET_LIBRARY}
#     ${POCO_FOUNDATION_LIBRARY}
#     ${ZLOG_LIBRARY}
#     ${JSONCPP_LIBRARY}
#     pthread
#     dl
#     rt
# )

# # 链接库
# target_link_libraries(mosquitto_client
#     ${MOSQUITTO_LIBRARY}
#     pthread
#     dl
#     rt
# )

# # 链接库
# target_link_libraries(zlog_demo
#     ${ZLOG_LIBRARY}
#     pthread
#     dl
#     rt
# )
# # 链接库
# target_link_libraries(linenoise_demo
#     ${LINENOISE_LIBRARY}
#     pthread
#     dl
#     rt
# )
# 查找所有源文件
file(GLOB_RECURSE SOURCE_FILES
    "src/*.cpp"
    "src/*.c"
)
add_executable(app_main  ${SOURCE_FILES})

target_link_libraries(app_main
    ${POCO_NET_LIBRARY}
    ${POCO_FOUNDATION_LIBRARY}
    ${ZLOG_LIBRARY}
    ${JSONCPP_LIBRARY}
    ${LINENOISE_LIBRARY}
    ${MOSQUITTO_LIBRARY}
    pthread
    dl
    rt
)